package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.POSProdTemplate;

public class POSProdTemplatePresenter extends AbstractPresenter<POSProdTemplate> {
	final String SINGLEITEM = "posProdTemplate";
	final String MULTIITEM = "pOSProdTemplates";
	
	public ArrayList<POSProdTemplate> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public POSProdTemplate tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public POSProdTemplate createItemFromJSONObject(JSONObject jsonProduct){
		POSProdTemplate product = new POSProdTemplate();
		try{
			product.setId(jsonProduct.getInt("id"));
			product.setPrice(jsonProduct.getDouble("price"));
			//Build Beer
			if(!jsonProduct.isNull("glassware")){
				POSGlassWarePresenter gwPresenter = new POSGlassWarePresenter();
				JSONObject jsonGW = jsonProduct.getJSONObject("glassware");
				product.setGlassware(gwPresenter.createItemFromJSONObject(jsonGW));
			} else {
				product.setGlassware(null);
			}
			return product;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(POSProdTemplate item){
		try{
			JSONObject json = new JSONObject();
			json.put("id", item.getId());
			json.put("price", item.getPrice());
			if(item.getGlassware()!=null){
				POSGlassWarePresenter gwPresenter = new POSGlassWarePresenter();
				json.put("glassware", gwPresenter.createJSON(item.getGlassware()));
			} else {
				json.put("glassware", JSONObject.NULL);
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSProdTemplate getEmptyEntity(){
		POSProdTemplate entity = new POSProdTemplate();
		return entity;
	}
}
